package com.fjt.book_trade.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fjt.book_trade.pojo.Order;
import com.fjt.book_trade.service.OrderService;
import com.fjt.book_trade.service.ProductService;

@Controller
public class OrderController {
	private ProductService productService;
	private OrderService orderService;
	public ProductService getProductService() {
		return productService;
	}
	@Resource(name="productservice")
	public void setProductService(ProductService productService) {
		this.productService = productService;
	}
	public OrderService getOrderService() {
		return orderService;
	}
	@Resource(name="orderservice")
	public void setOrderService(OrderService orderService) {
		this.orderService = orderService;
	}
	
	
	@RequestMapping("/order/getuserorder") 
    @ResponseBody
    public ModelAndView getUserOrder(HttpServletRequest request){
    	Map<String,Object> modelMap=new HashMap<String,Object>();
    	
    	ModelAndView modelView=new ModelAndView(); 
    	HttpSession session=request.getSession();
    	String username=(String)session.getAttribute("username");
    	if(username!=null){
    		modelMap=orderService.getUserOrderlist(username);
        	modelView.addAllObjects(modelMap);
        	System.out.println("/order/getuserorder");
        	return modelView;
    	}else{
    		modelMap.put("result", "fail");
    		modelView.addAllObjects(modelMap);
    		System.out.println("/order/getuserorder no session");
    		return modelView;
    	}
    }
    
	@RequestMapping("/order/getsellorder") 
    @ResponseBody
    public ModelAndView getMySellOrder(HttpServletRequest request){
    	Map<String,Object> modelMap=new HashMap<String,Object>();
    	
    	ModelAndView modelView=new ModelAndView(); 
    	HttpSession session=request.getSession();
    	String username=(String)session.getAttribute("username");
    	if(username!=null){
    		modelMap=orderService.getMySellOrderlist(username);
        	modelView.addAllObjects(modelMap);
        	System.out.println("/order/getsellorder");
        	return modelView;
    	}else{
    		modelMap.put("result", "fail");
    		modelView.addAllObjects(modelMap);
    		System.out.println("/order/getsellorder no session");
    		return modelView;
    	}
    }
	
    @RequestMapping("/order/getproductorder") 
    @ResponseBody
    public ModelAndView getProductOrder(@RequestBody Order order,HttpServletRequest request){  
    	Map<String,Object> modelMap=new HashMap<String,Object>();
    	
    	ModelAndView modelView=new ModelAndView(); 
    	HttpSession session=request.getSession();
    	String username=(String)session.getAttribute("username");
    	if(username!=null){
    		modelMap=orderService.getProductOrderlist(order.getProduct_id());
        	modelView.addAllObjects(modelMap); 
        	System.out.println("/order/getproductorder");
        	return modelView;
    	}else{
    		modelMap.put("result", "fail");
    		modelView.addAllObjects(modelMap);
    		System.out.println("/order/getproductorder no session");
    		return modelView;
    	}
    	
    }
    
    @RequestMapping("/order/changeorderstate") 
    @ResponseBody
    public ModelAndView changeOrderState(@RequestParam("order_id")int order_id,@RequestParam("state")String state,HttpServletRequest request,HttpServletResponse response){
    	Map<String,String> modelMap=new HashMap<String,String>();
    	
    	ModelAndView modelView=new ModelAndView(); 
    	HttpSession session=request.getSession();
    	String username=(String)session.getAttribute("username");
    	if(username!=null){	
        	if(state.equals("已取消"))
        		productService.BackProduct(orderService.getUserOrderById(order_id).getProduct_id(), orderService.getUserOrderById(order_id).getProduct_number());
        	else if(state.equals("已付款")){
//        		response.sendRedirect(location);
        	}else if(state.equals("已发货")){
        		 
        	}else if(state.equals("已收货")){
        		
        	}
        	
        	modelMap=orderService.changeOrderState(order_id, state);
        	modelView.addAllObjects(modelMap);
        	System.out.println("/order/changeorderstate");
        	return modelView;
    	}else{
    		modelMap.put("result", "fail");
    		modelView.addAllObjects(modelMap);
    		System.out.println("/order/changeorderstate no session");
    		return modelView;
    	}
    	
    }
    
    @RequestMapping(value="/order/assessorder") 
    @ResponseBody
    public ModelAndView assessorder(@RequestBody Order order,HttpServletRequest request){  
    	Map<String,Object> modelMap=new HashMap<String,Object>();
    	
    	ModelAndView modelView=new ModelAndView(); 
    	HttpSession session=request.getSession();
    	String username=(String)session.getAttribute("username");
    	if(username!=null){
    		modelMap=orderService.AssessOrder(order.getOrder_id(), order.getAssess());
        	modelView.addAllObjects(modelMap);
        	System.out.println("/order/assessorder");
        	return modelView;
    	}else{
    		modelMap.put("result", "fail");
    		modelView.addAllObjects(modelMap);
    		System.out.println("/order/assessorder no session");
    		return modelView;
    	}
    }
    

	
}
